/*
 * Filename:    WorkFlowServiceImpl.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014年10月29日
 */
package com.paic.mhis.hcpms.warning.biz.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.paic.mhis.common.util.BusinessServiceUtil;
import com.paic.mhis.hcpms.common.workflow.biz.service.HcpmsWFService;
import com.paic.mhis.hcpms.common.workflow.dto.WorkFlowGtasksDTO;
import com.paic.mhis.hcpms.warning.biz.service.WarningInfoService;
import com.paic.mhis.hcpms.warning.dto.PaginationQueryDTO;
import com.paic.mhis.hcpms.warning.dto.WarningGtasksDTO;
import com.paic.mhis.hcpms.warning.intergration.dao.WarningInfoDao;
import com.paic.mhis.hcpms.warning.util.WarnConstants;
import com.paic.pafa.app.biz.service.BusinessServiceException;

@Component("warningInfoService")
public class WarningInfoServiceImpl implements WarningInfoService {
	@Autowired
	private HcpmsWFService hcpmsWFService;
	@Autowired
	private WarningInfoDao warningInfoDao;

	/**
	 * 获取预警节点信息
	 */
	@Override
	public Map<String, Object> getWarnActinst() throws BusinessServiceException {
		// TODO Auto-generated method stub
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			result.put(WarnConstants.SUCCESS_RESULT_KEY, hcpmsWFService.getActinstInfo(WarnConstants.TASK_TYPE));
			result.put(WarnConstants.STATE_KEY, WarnConstants.SUCCESS_STATE_VALUE);
			result.put(WarnConstants.MSG_KEY, WarnConstants.SELECT_SUCCESS);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 获取待办任务
	 * 
	 * @throws BusinessServiceException
	 */
	@Override
	public Map<String, Object> getGTasks(PaginationQueryDTO taskCondition) throws BusinessServiceException {
		// TODO Auto-generated method stub
		Map<String, Object> result = new HashMap<String, Object>();
		List<WarningGtasksDTO> taskList = new ArrayList<WarningGtasksDTO>();
		try {
			int count = warningInfoDao.getGtasksCount(taskCondition.getParam());
			if (count > 0) {
				taskList = warningInfoDao.getGtasks(taskCondition);
			}
			result.put(WarnConstants.SUCCESS_RESULT_KEY, taskList);
			result.put(WarnConstants.TOTAL, count);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 获取预警任务
	 * 
	 * @throws BusinessServiceException
	 */
	@Override
	public Map<String, Object> getWarnList(String assigner, List<String> role, PaginationQueryDTO taskCondition)
			throws BusinessServiceException {
		// TODO Auto-generated method stub
		Map<String, Object> result = new HashMap<String, Object>();
		List<WarningGtasksDTO> taskList = new ArrayList<WarningGtasksDTO>();
		try {
			int count = warningInfoDao.getWarnListCount(taskCondition.getParam());
			if (count > 0) {
				taskList = warningInfoDao.getWarnList(taskCondition);
				// taskList = getOperatorTran(assigner, role, taskList);
			}
			result.put(WarnConstants.SUCCESS_RESULT_KEY, taskList);
			result.put(WarnConstants.TOTAL, count);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 获取可操作的跳转
	 * 
	 * @param assigner
	 * @param role
	 * @param taskDtoList
	 * @return
	 */
	private List<WarningGtasksDTO> getOperatorTran(String assigner, List<String> role,
			List<WarningGtasksDTO> taskDtoList) {
		List<String> taskList = new ArrayList<String>();
		for (WarningGtasksDTO task : taskDtoList) {
			taskList.add(task.getWarnNo());
		}
		List<WorkFlowGtasksDTO> tranList = hcpmsWFService.getOperatorTran(assigner, role, taskList);
		for (int i = 0; i < taskDtoList.size(); i++) {
			WarningGtasksDTO task = taskDtoList.get(i);
			taskDtoList.get(i).setTranName("查看");
			for (WorkFlowGtasksDTO tran : tranList) {
				if (tran.getTaskNo().equals(task.getWarnNo())) {
					taskDtoList.get(i).setTranName(tran.getTranName());
					break;
				}
			}
		}
		return taskDtoList;
	}

}
